<?php

/* HOST PAPA MEMORY LIMIT */
ini_set('memory_limit', '64M');


define('INI_FILE', 'application.ini');
define('DOCUMENT_ROOT', dirname(dirname(__FILE__)));
define('PUBLIC_ROOT', dirname(__FILE__));
define('ROOT_MAIL', 'no-reply@site_host.com');
// Load framework and document
require_once(DOCUMENT_ROOT . '/library/Erra/load.inc.php');

/*
  RAPPEL SMS CLIENT - Envoyer un SMS au client pour lui rappeler l'heure de son rendez-vous
 * ---------------------------------------------------------------------------------
 */
$sms_client_reminder = Config_GeneralConfigObject::get('sms_client_reminder');
if ($sms_client_reminder > 0) {
    $query = 'SELECT * FROM msa_slotobject WHERE (slot_day+slot_start) > ' . (int) (time()) . ' AND (slot_day+slot_start) < ' . (int) (time() + ($sms_client_reminder * 3600));
    $nextRdv = Erra_Core_DatabasePdo::SELECT($query);
    if (Erra_Core_DatabasePdo::rowCount()) {
        while ($futureRdv = $nextRdv->fetch()) {
            // Check if the task has already been executed
            $checkQuery = Erra_Core_DatabasePdo::SELECT('SELECT * FROM cron WHERE event_id="' . $futureRdv->event_id . '" AND action="sms_client_reminder"');
            if (!Erra_Core_DatabasePdo::rowCount()) {
                // Send SMS
                // Get client phone
                $phone = Erra_Core_DatabasePdo::SELECT('SELECT telephone_portable FROM msa_clientobject WHERE id=' . $futureRdv->client_id);
                if (Erra_Core_DatabasePdo::rowCount() && is_object($phone)) {
                    $phone = $phone->fetch();
                    $telephone_portable = $phone->telephone_portable;

                    $nom_etablissement = Config_GeneralConfigObject::get('nom_etablissement');
                    $date_reservation = date('d/m/Y', $futureRdv->slot_day);
                    $heure_reservation = time_to_str_hour($futureRdv->slot_start);
                    $placeHolders = array('{nom_etablissement}' => $nom_etablissement, '{date_reservation}' => $date_reservation, '{heure_reservation}' => $heure_reservation);
                    Erra_Utils_Tools::sendTemplatedSms($telephone_portable, 'Sms/Sms_RdvReminderUser.phtml', $placeHolders);

                    // Now mark this in CRON database
                    Erra_Core_DatabasePdo::EXECUTE('INSERT INTO cron(event_id, action, exec_date) VALUES("' . $futureRdv->event_id . '", "sms_client_reminder", "' . time() . '")');
                }
            }
        }
    }
}


/*
  RAPPEL MAIL CLIENT - Envoyer un mail au client pour lui rappeler l'heure de son rendez-vous
 * ---------------------------------------------------------------------------------
 */
$mail_client_reminder = Config_GeneralConfigObject::get('mail_client_reminder');
if ($mail_client_reminder > 0) {
    $query = 'SELECT * FROM msa_slotobject WHERE (slot_day+slot_start) > ' . (int) (time()) . ' AND (slot_day+slot_start) < ' . (int) (time() + ($mail_client_reminder * 3600));
    $nextRdv = Erra_Core_DatabasePdo::SELECT($query);
    if (Erra_Core_DatabasePdo::rowCount()) {
        while ($futureRdv = $nextRdv->fetch()) {
            // Check if the task has already been executed
            $checkQuery = Erra_Core_DatabasePdo::SELECT('SELECT * FROM cron WHERE event_id="' . $futureRdv->event_id . '" AND action="mail_client_reminder"');
            if (!Erra_Core_DatabasePdo::rowCount()) {
                // Send MAIL
                // Get client mail
                $mail = Erra_Core_DatabasePdo::SELECT('SELECT email, nom, prenom FROM msa_clientobject WHERE id=' . $futureRdv->client_id);
                if (Erra_Core_DatabasePdo::rowCount() && is_object($mail)) {
                    $mail = $mail->fetch();
                    $email_address = $mail->email;

                    $nom_etablissement = Config_GeneralConfigObject::get('nom_etablissement');
                    $url_reservation = Config_GeneralConfigObject::get('url_reservation');
                    $date_reservation = date('d/m/Y', $futureRdv->slot_day);
                    $heure_reservation = time_to_str_hour($futureRdv->slot_start);
                    $nom_complet = $mail->nom . ' ' . $mail->prenom;
                    $placeHolders = array('{nom_etablissement}' => $nom_etablissement,
                        '{date_reservation}' => $date_reservation,
                        '{heure_reservation}' => $heure_reservation,
                        '{url_reservation}' => $url_reservation,
                        '{nom_complet}' => $nom_complet);
                    Erra_Utils_Tools::sendTemplatedMail($email_address, 'Mail/Mail_RdvReminderClient.phtml', $placeHolders);
                    // Now mark this in CRON database
                    Erra_Core_DatabasePdo::EXECUTE('INSERT INTO cron(event_id, action, exec_date) VALUES("' . $futureRdv->event_id . '", "mail_client_reminder", "' . time() . '")');
                }
            }
        }
    }
}


/*
  RAPPEL SMS EQUIPE - Envoyer un SMS a l'équipe pour rappel du rendez-vous
 * ---------------------------------------------------------------------------------
 */
$sms_team_reminder = Config_GeneralConfigObject::get('sms_team_reminder');
if ($sms_team_reminder > 0) {
    $query = 'SELECT * FROM msa_slotobject WHERE (slot_day+slot_start) > ' . (int) (time()) . ' AND (slot_day+slot_start) < ' . (int) (time() + ($sms_team_reminder * 3600));
    $nextRdv = Erra_Core_DatabasePdo::SELECT($query);
    if (Erra_Core_DatabasePdo::rowCount()) {
        while ($futureRdv = $nextRdv->fetch()) {
            // Check if the task has already been executed
            $checkQuery = Erra_Core_DatabasePdo::SELECT('SELECT * FROM cron WHERE event_id="' . $futureRdv->event_id . '" AND action="sms_team_reminder"');
            if (!Erra_Core_DatabasePdo::rowCount()) {
                // Send SMS
                // Get client phone
                $phone = Erra_Core_DatabasePdo::SELECT('SELECT nom_agenda, msa_agendaobject.telephone_portable AS telephone_portable, msa_clientobject.nom, msa_clientobject.prenom FROM msa_agendaobject, msa_clientobject WHERE msa_agendaobject.id=' . $futureRdv->agenda_id . ' AND msa_clientobject.id=' . $futureRdv->client_id);
                if (Erra_Core_DatabasePdo::rowCount() && is_object($phone)) {
                    $phone = $phone->fetch();
                    $telephone_portable = $phone->telephone_portable;

                    $nom_etablissement = Config_GeneralConfigObject::get('nom_etablissement');
                    $date_reservation = date('d/m/Y', $futureRdv->slot_day);
                    $heure_reservation = time_to_str_hour($futureRdv->slot_start);
                    $nom_complet = $phone->nom . ' ' . $phone->prenom;
                    $placeHolders = array('{nom_etablissement}' => $nom_etablissement,
                        '{date_reservation}' => $date_reservation,
                        '{heure_reservation}' => $heure_reservation,
                        '{nom_complet}' => $nom_complet);
                    Erra_Utils_Tools::sendTemplatedSms($telephone_portable, 'Sms/Sms_RdvReminderPresta.phtml', $placeHolders);
                    // Now mark this in CRON database
                    Erra_Core_DatabasePdo::EXECUTE('INSERT INTO cron(event_id, action, exec_date) VALUES("' . $futureRdv->event_id . '", "sms_team_reminder", "' . time() . '")');
                }
            }
        }
    }
}

/*
  RAPPEL MAIL EQUIPE - Envoyer un aux équipes avant le rendez vous
 * ---------------------------------------------------------------------------------
 */
$mail_team_reminder = Config_GeneralConfigObject::get('mail_team_reminder');
if ($mail_team_reminder > 0) {
    $query = 'SELECT * FROM msa_slotobject WHERE (slot_day+slot_start) > ' . (int) (time()) . ' AND (slot_day+slot_start) < ' . (int) (time() + ($mail_team_reminder * 3600));
    $nextRdv = Erra_Core_DatabasePdo::SELECT($query);
    if (Erra_Core_DatabasePdo::rowCount()) {
        while ($futureRdv = $nextRdv->fetch()) {
            // Check if the task has already been executed
            $checkQuery = Erra_Core_DatabasePdo::SELECT('SELECT * FROM cron WHERE event_id="' . $futureRdv->event_id . '" AND action="mail_team_reminder"');
            if (!Erra_Core_DatabasePdo::rowCount()) {
                // Send MAIL
                // Get member mail
                $mail = Erra_Core_DatabasePdo::SELECT('SELECT nom_agenda, msa_agendaobject.email, msa_clientobject.nom, msa_clientobject.prenom FROM msa_agendaobject, msa_clientobject WHERE msa_agendaobject.id=' . $futureRdv->agenda_id . ' AND msa_clientobject.id=' . $futureRdv->client_id);
                if (Erra_Core_DatabasePdo::rowCount() && is_object($mail)) {
                    $mail = $mail->fetch();
                    $email_address = $mail->email;
                    $nom_etablissement = Config_GeneralConfigObject::get('nom_etablissement');
                    $url_reservation = Config_GeneralConfigObject::get('url_reservation');
                    $date_reservation = date('d/m/Y', $futureRdv->slot_day);
                    $heure_reservation = time_to_str_hour($futureRdv->slot_start);
                    $nom_complet = $mail->nom . ' ' . $mail->prenom;
                    $nom_agenda = $mail->nom_agenda;
                    $placeHolders = array('{nom_etablissement}' => $nom_etablissement,
                        '{date_reservation}' => $date_reservation,
                        '{heure_reservation}' => $heure_reservation,
                        '{url_reservation}' => $url_reservation,
                        '{nom_complet}' => $nom_complet,
                        '{nom_agenda}' => $nom_agenda);
                    Erra_Utils_Tools::sendTemplatedMail($email_address, 'Mail/Mail_RdvReminderPresta.phtml', $placeHolders);
                    // Now mark this in CRON database
                    Erra_Core_DatabasePdo::EXECUTE('INSERT INTO cron(event_id, action, exec_date) VALUES("' . $futureRdv->event_id . '", "mail_team_reminder", "' . time() . '")');
                }
            }
        }
    }
}
// Delete 5 days old events
$safe_delete_cron = 'SELECT id FROM `cron` WHERE (exec_date+' . (int) (3600 * 24 * 5) . ') < '.time();
$safeDeleteCrons = Erra_Core_DatabasePdo::SELECT($safe_delete_cron);
if(Erra_Core_DatabasePdo::rowCount()) {
    while($cronToDelete = $safeDeleteCrons->fetch()) {
        Erra_Core_DatabasePdo::EXECUTE('DELETE FROM cron WHERE id='.$cronToDelete->id);
    }
}

// TODO : Delete automatically trash every week

?>